/**
 * Copyright &copy; 2018 thinkis All rights reserved.
 */
package com.thinkis.modules.sys.web;

import com.thinkis.common.utils.AjaxJson;
import com.thinkis.common.persistence.Page;
import com.thinkis.common.utils.StringUtils;
import com.thinkis.common.web.BaseController;
import com.thinkis.common.web.Result;
import com.thinkis.common.web.ResultGenerator;
import com.thinkis.modules.sys.entity.Schedule;
import com.thinkis.modules.sys.service.ScheduleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 系统定时任务Controller
 * @author jauddy
 * @version 2019-07-05
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/schedule")
public class ScheduleController extends BaseController {

	@Autowired
	private ScheduleService scheduleService;
	
	@ModelAttribute
	public Schedule get(@RequestParam(required=false) String id) {
		Schedule entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = scheduleService.get(id);
		}
		if (entity == null){
			entity = new Schedule();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:schedule:view")
	@RequestMapping(value = {"list", ""})
	public String list(Schedule schedule, HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("schedule", schedule);
		return "modules/sys/scheduleList";
	}

	@RequiresPermissions("sys:schedule:view")
	@RequestMapping(value = "listAjax",method=RequestMethod.GET)
	@ResponseBody
	public Result listAjax(Schedule schedule, HttpServletRequest request, HttpServletResponse response) {
		Page<Schedule> page = scheduleService.findPage(new Page<Schedule>(request, response), schedule);
		return ResultGenerator.genSuccessResult(page.getCount(),page.getList());
	}

	@RequiresPermissions("sys:schedule:view")
	@RequestMapping(value = "form")
	public String form(Schedule schedule, Model model) {
		model.addAttribute("schedule", schedule);
		return "modules/sys/scheduleForm";
	}
	
	@RequiresPermissions("sys:schedule:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public Result save(Schedule schedule) {
		scheduleService.save(schedule);
		return ResultGenerator.genSuccessResult();
	}
	
	@RequiresPermissions("sys:schedule:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public Result delete(Schedule schedule) {
		scheduleService.delete(schedule);
		return ResultGenerator.genSuccessResult();
	}
	
	@RequiresPermissions("sys:schedule:edit")
	@RequestMapping(value = "batchDelete")
	@ResponseBody
	public Result batchDelete(String selectedIds) {
		int success = scheduleService.batchDelete(selectedIds);
		return ResultGenerator.genSuccessResult("成功删除"+success+"条数据");
	}


    /**
     * 暂停任务
     */
    @RequiresPermissions("sys:schedule:stop")
    @RequestMapping(value="stop")
    @ResponseBody
    public Object stop(Schedule schedule) {
        AjaxJson j = new AjaxJson();
        scheduleService.stopJob(schedule);
        j.setSuccess(true);
        j.setMsg("暂停成功!");
        return j;
    }


    /**
     * 立即运行一次
     */
    @RequiresPermissions("sys:schedule:startNow")
    @RequestMapping("startNow")
    @ResponseBody
    public AjaxJson stratNow(Schedule schedule) {
        AjaxJson j = new AjaxJson();
        j.setSuccess(true);
        j.setMsg("运行成功");
        scheduleService.startNowJob(schedule);
        return j;
    }

    /**
     * 恢复
     */
    @RequiresPermissions("sys:schedule:resume")
    @RequestMapping("resume")
    @ResponseBody
    public AjaxJson resume(Schedule schedule, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        j.setSuccess(true);
        j.setMsg("恢复成功");
        scheduleService.restartJob(schedule);
        scheduleService.startNowJob(schedule);
        //恢复之后，立即触发一次激活定时任务，不然定时任务有可能不会执行，这是bug的回避案，具体原因我没找到。
        return j;
    }

    /**
     * 验证类任务类是否存在
     */
    @RequestMapping("existsClass")
    @ResponseBody
    public boolean existsClass(String className) {
        Class job = null;
        try {
            job = Class.forName(className);
            return true;
        } catch (ClassNotFoundException e1) {
            return false;
        }
    }

}